package tech.yunland.shop.domain.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.yunland.shop.domain.dao.ExamDescriptionDao;
import tech.yunland.shop.domain.model.ExamDescription;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * Created by v on 17-7-6.
 */
@Service
public class ExamDescriptionServiceImpl implements ExamDescriptionService {

    @Autowired
    private ExamDescriptionDao examDescriptionDao;

    @Override
    public ExamDescription save(String des) {
        ExamDescription examDescription = new ExamDescription();
        examDescription.setExamDescription(des);
        examDescriptionDao.saveOrUpdate(examDescription);
        return examDescription;
    }

    @Override
    public Integer getSize(String param) {
        String sql =
                "select count(1) counts\n" +
                        "from\n" +
                        "exam_description ex\n";
        int counts = ((BigInteger) examDescriptionDao.findUniqueBySQL(sql).get("counts")).intValue();
        return counts;
    }

    @Override
    public List<ExamDescription> get(String param, Integer startIdx, Integer rows) {
        String hql = "from ExamDescription ex\n";
        return examDescriptionDao.find(hql, startIdx, rows);
    }

    @Override
    public Integer getDesSize(String param) {
        String sql =
                "select count(1) counts\n" +
                        "from\n" +
                        "(SELECT\n" +
                        "  DISTINCT\n" +
                        "  e.exam_time,\n" +
                        "  ed.examdescription\n" +
                        "FROM exam e LEFT JOIN exam_description ed ON e.examdescription_id = ed.id) as a\n";
        int counts = ((BigInteger) examDescriptionDao.findUniqueBySQL(sql).get("counts")).intValue();
        return counts;
    }

    @Override
    public List<Map<String, Object>> getDes(String param, Integer startIdx, Integer rows) {
        String sql = "SELECT\n" +
                "  DISTINCT\n" +
                "  e.exam_time,\n" +
                "  ed.examdescription,\n" +
                "  e.examdescription_id\n" +
                "FROM exam e LEFT JOIN exam_description ed ON e.examdescription_id = ed.id";
        return examDescriptionDao.findBySQL(sql, startIdx, rows);
    }
}
